import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios'
import { Toast } 	from 'vant';
Vue.use(Toast);

export default {
	name: 'transfer',
	data(){
		return {
			amount:'',				//划转金额
			limitAmount:0,			//最小划转金额
			btnBgColor:'#1C4D7F',	//提交验证码按钮颜色
			loading:false,	//按钮是否加载中
			balance:'',
			defaultIcon:'USDT'
		}
	},
	watch:{
		amount(newVal){
			//最多小数点后保留2位
			if(typeof newVal === 'number') newVal = newVal.toString();
			if(newVal && newVal.indexOf('.') > -1){
				let arrNewVal = newVal.split('.');
				if(arrNewVal.length > 0 && arrNewVal[1].length > 2){
					arrNewVal[1] = arrNewVal[1].substr(0,2);
					this.amount = arrNewVal.join('.');
				}
			}
		}
	},
	computed:{
		allAmount(){
			//总余额
			if(!this.balance) return 0;
			let _filter = this.balance.commission.list.filter(item => item.symbol === this.defaultIcon);
			return util.toNonExponential(_filter.length > 0?_filter[0].amount:0);
		},
	},
	methods:{
		refreshNative(){
			util.callHandler('userBalanceChanged',{},res => {
				console.log(res)
			})
		},
		upload(){
			if(isNaN(this.amount)) return
			if(Number(this.amount) < Number(this.limitAmount)){
				// Toast(`返佣划转金额不低于${this.limitAmount}USDT`);
				Toast(this.$t('transfer-key14',{amount:this.limitAmount}))
				return
			}
			if(Number(this.amount) > Number(this.allAmount)){
				// Toast(`返佣划转金额不高于${this.allAmount}USDT`);
				Toast(this.$t('transfer-key15',{amount:this.allAmount}))
				return
			}
			this.loading = true;
			util.fetch(`${util.ajaxUrl}/transfer/partner`,{
				token:this.token || util.getToken(),
				amount:this.amount
			}).then(res => {
				this.loading = false;
				if(res.retCode === 0){
					this.refreshNative();
					Toast(res.retMsg || this.$t('success'));
					this.amount = '';
					this.init()
				}else{
					Toast(res.retMsg || this.$t('noError'))
				}
			}).catch(error => {
				this.loading = false;
				console.log(error)
			})
		},
		init(){
			util.fetch(`${util.ajaxUrl}/user/balance`,{
				token:this.token || util.getToken(),
			}).then(res => {
				if(res.retCode === 0){
					this.balance = res.retData;
				}else{
					Toast(res.retMsg || this.$t('noError'))
				}
			}).catch(error => {
				console.log(error)
			})

			//读取返佣最小划转金额
			util.siteConfig(this.token || util.getToken()).then(res => {
				this.limitAmount = res.min_rebate_amount;
			})
		},
		goHistory(){
			this.$router.push({
				path:'/history',
				query:{
					title:this.$t('transfer-key1')
				}
			})
		},
		amountBlur(){
			window.scrollTo(0, 0);
		},
		sellAll(){
			this.amount = this.allAmount;
		},
		goBack(){
			util.navigateBack(-1,this)
		},
		stop(){}
	},
	created() {
		util.waitSDKReady().then(token => {
			this.token = token;
			this.init();
		}).catch((e) => {
			Toast(this.$t('getTokenFail'))
		})
		setTimeout(() => {
			util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
				if(this.token) this.init();
				util.callHandler('setStatusBarStyle',{
					style:'light'
				})
			})
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				window.token = '';
				this.token = '';
				localStorage.removeItem('token');
			})
			util.callHandler('setStatusBarStyle',{
				style:'light'
			})
		},500)
    }
}